<template>
	<view class="main">
		<uni-nav-bar title="订单中心" background-color="#007AFF" color="#000" statusBar="true"></uni-nav-bar>
		<view class="top-view">
			<uni-segmented-control :current="current" :values="currentItems" styleType="text"
				@clickItem="onClickItem" />
			<view class="search-box">
				<view class="search-item">
					<view class="twoitem">
						<uni-easyinput :inputBorder="false" v-model="searchForm.executeNo" placeholder="输入执行单号检索" @clear="reset"
							@blur="changeorderNo"></uni-easyinput>
					</view>
					<button class="searchbutton" @click="changeorderNo">
						<text>搜索</text>
					</button>
				</view>
			</view>
		</view>
		<scroll-view class="scrollView" scroll-y="true" @scrolltolower="handleScrolltolowerone()"
			:style="{height: setcollapseheight() + 'px'}">
			<OrderItem @processing="processing" :dataList="dataList"></OrderItem>
			<view v-if="!dataList.length">
				<u-empty mode="message" icon="http://cdn.uviewui.com/uview/empty/message.png" class="empty-message">
				</u-empty>
			</view>
		</scroll-view>
		<u-modal :closeOnClickOverlay="true" cancelColor="#e74436" confirmText="同意" cancelText="拒绝" buttonReverse
			showCancelButton @close="cancel" @cancel="refused" @confirm="confirm" :show="modalshow" :title="modalTilte">
			<view class="slot-content">
				<view>
					<view>
						项目订单编号：{{itemForm.orderNo}}
					</view>
					<view>
						项目订单名称：{{itemForm.name}}
					</view>
					<view v-if="modalTilte==='订单关闭申请'">
						关闭原因：{{itemForm.applyReason}}
					</view>
					<view>
						申请日期：{{itemForm.applyDate}}
					</view>
				</view>
			</view>
		</u-modal>
		<u-modal cancelColor="#e74436" confirmText="确定" cancelText="返回" showCancelButton @cancel="cancelSon"
			@confirm="confirmSon" :show="modalsonshow" :title="modalsonTilte">
			<view class="slot-content">
				<view v-if="modalsonTilte!=='拒绝申请'" style="height: 100rpx;line-height: 100rpx;">
					确定同意订单申请吗？
				</view>
				<view v-if="modalsonTilte==='拒绝申请'"
					style="width: 100%;min-height: 100rpx;display: flex;justify-content: space-between;align-items: center;">
					拒绝原因：<uni-easyinput type="text" v-model="reason" placeholder="请输入拒绝原因" />
				</view>
			</view>
		</u-modal>
		<!-- //拒绝订单 -->
		<u-modal cancelColor="#e74436" confirmText="确定" cancelText="返回" showCancelButton @cancel="modalrefuseshow=false"
			@confirm="confirmrefuse" :show="modalrefuseshow" :title="modalrefusetitle">
			<view class="slot-content">
				<view v-if="modalrefusetitle == '申请完成'" style="height: 100rpx;line-height: 100rpx;">
					确定要申请完成吗？
				</view>
				<view v-if="modalrefusetitle == '申请中止'"
					style="width: 100%;min-height: 100rpx;display: flex;justify-content: space-between;align-items: center;">
					<!-- 中止原因：<uni-easyinput type="text" v-model="reason" placeholder="请输入中止原因" /> -->
					<view style="font-size: 24rpx;width:400rpx; ">
						<view style="margin-bottom: 16rpx;">
							<span style="color: red;">*</span>中止原因：
						</view>
						<uni-easyinput v-model="reason" placeholder="请输入中止原因" type="textarea"></uni-easyinput>
					</view>
				</view>
			</view>
		</u-modal>
		<bottomBar ref="bottomBar"></bottomBar>
	</view>
</template>

<script>
	import bottomBar from "@/components/socialized-bottomtabar/home-bottomtabar"
	import orderCenterApi from '@/api/socialized-servce/order-center/index.js'
	import OrderItem from "./components/order-item.vue"
	export default {
		components: {
			bottomBar,
			OrderItem
		},
		data() {
			return {
				current: 0,
				currentItems: ['全部', '待接单', '待变更', '已接单', '已完成', '已中止'],
				page: 1,
				size: 10,
				total: 0,
				totalPages: 0,
				dataList: [],
				searchForm: {},
				batchList: [],
				modalshow: false,
				modalTilte: '',
				statusList: null,
				itemForm: {}, //处理订单申请
				modalsonTilte: '',
				modalsonshow: false,
				reason: '', //拒绝
				modalrefuseshow: false, //取消订单
				modalrefusetitle: '', //取消订单
			}
		},
		onShow() {
			uni.hideTabBar()
		},
		mounted() {
			this.setcollapseheight()
			this.queryPageList()
		},
		methods: {
			processing(item, params) {
				console.log(item, params);
				if (['申请完成', '申请中止'].includes(params.title)) {
					this.itemForm = {}
					this.modalrefuseshow = true
					this.modalrefusetitle = params.title
					this.itemForm = JSON.parse(JSON.stringify(item))
				} else if (params.title == '审核订单修改') {
					let params = {
						data: JSON.stringify({
							pageTitle: '审核订单修改',
							...item
						})
					}
					let url = '/pages/socialized-service/order-center/quotation-appointment-edit'
					this.$togo(url, params)
				} else if (params.title == '工单管理') {
					uni.setStorageSync('workOrderInfo', JSON.stringify(item))
					let url = '/pages/socialized-service/work-order/worker-order-list'
					this.$togo(url, {
						reportExecuteId: item.id
					})
				} else {
					this.itemForm = {}
					this.modalTilte = params.title
					this.modalshow = params.modalshow
					this.itemForm = JSON.parse(JSON.stringify(item))
				}
			},
			refused() {
				let params = this.itemForm
				this.modalsonshow = true
				this.modalsonTilte = '拒绝申请'
			},
			cancel() {
				this.modalshow = false
			},
			confirm() {
				this.modalsonTilte = '同意'
				this.modalsonshow = true
			},
			cancelSon() {
				this.modalsonshow = false
			},
			confirmSon() {
				let params = this.itemForm
				console.log(params)
				if (this.modalsonTilte === '同意') {
					orderCenterApi.agree(params.projectNo).then(res => {
						console.log(res);
						if (res.code == 200) {
							setTimeout(() => {
								this.modalsonshow = false
								this.modalshow = false
								this.queryPageList()
							}, 1500)
						} else {
							setTimeout(() => {
								this.modalsonshow = false
							}, 1500)
						}
						uni.showToast({
							title: res.code == 200 ? res.msg || '操作成功' : res.msg || '操作失败',
							icon: 'none'
						})
					})
				} else {
					if (!this.reason) {
						uni.showToast({
							title: '请输入拒绝原因',
							icon: 'none'
						})
						return
					}
					orderCenterApi.refuse(params.projectNo, this.reason).then(res => {
						console.log(res);
						if (res.code == 200) {
							setTimeout(() => {
								this.modalsonshow = false
								this.modalshow = false
								this.queryPageList()
							}, 1500)
						} else {
							setTimeout(() => {
								this.modalsonshow = false
							}, 1500)
						}
						uni.showToast({
							title: res.code == 200 ? res.msg || '操作成功' : res.msg || '操作失败',
							icon: 'none'
						})
					})
				}
			},

			//取消订单
			confirmrefuse() {
				let executeNo = this.itemForm.executeNo
				if (this.modalrefusetitle == '申请完成') {
					orderCenterApi.applyCompleteExecute(executeNo).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '申请完成成功',
								icon: 'none'
							})
							this.modalrefuseshow = false
							this.queryPageList()
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					})
				} else if (this.modalrefusetitle == '申请中止') {
					if (!this.reason) {
						uni.showToast({
							title: '请输入中止原因',
							icon: 'none'
						})
						return
					}
					orderCenterApi.applyCloseExecute({
						executeNo,
						reason: this.reason || ''
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '申请中止成功',
								icon: 'none'
							})
							this.modalrefuseshow = false
							this.queryPageList()
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					})
				}
			},
			onClickItem(e) {
				if (this.current !== e.currentIndex) {
					this.page = 1
					this.current = e.currentIndex
					console.log(e);
					if (e.currentIndex === 0)  {
						this.statusList = null
					} else {
						this.statusList = e.currentIndex - 1
					}
					this.queryPageList()
				}
			},
			serviceChange(e) {
				console.log(e);
				this.page = 1
				this.queryPageList()
			},
			changeorderNo(e) {
				console.log(e);
				this.page = 1
				this.queryPageList()
			},
			//重置
			reset() {
				this.params = {}
				this.searchForm = {}
				this.page = 1
				this.queryPageList()
			},
			//委托下单
			clickRight(e) {
				console.log(e)
				let url = '/pages/socialized-service/order-center/entrusted-order'
				this.$togo(url)
			},
			async queryPageList() {
				let condition = {
					...this.searchForm,
					providerNo: uni.getStorageSync('providerNo'),
					organizationNo: uni.getStorageSync('organizationNo'),
				}
				if (this.statusList || this.statusList == 0) {
					condition.status = this.statusList
				}
				
				let params = {
					page: this.page,
					size: this.size,
					condition
				}
				uni.showLoading({
					title: '加载中...'
				});
				let res = await orderCenterApi.queryPageByCondition(params)
				uni.hideLoading();
				if (res.code === 200) {
					this.total = res.data.totalElements
					this.totalPages = res.data.totalPages
					let newList = res.data.content || []
					if (this.page === 1) {
						this.dataList = newList
					} else {
						this.dataList = [...this.dataList, ...newList]
					}
				}
			},
			setcollapseheight() {
				let topheightone = uni.upx2px(410)
				let winHeight = 0
				uni.getSystemInfo({
					success: function(res) {
						winHeight = res.windowHeight
					}
				});

				winHeight = parseInt(winHeight) - topheightone
				// #ifndef H5
				winHeight = winHeight - 20

				// #endif
				return winHeight
			},
			handleScrolltolowerone() {
				console.log('触底', this.page, this.totalPages);
				if (this.page <= this.totalPages && this.page * 10 < this.total) {
					this.page++
					this.queryPageList()
				}
			},
		}

	}
</script>

<style lang="scss" scoped>
	.top-view {
		padding: 0 20rpx;
	}
	.scrollView {
		// padding-top: 20rpx;
		padding: 20rpx;
		box-sizing: border-box;

		.content {
			width: 100%;
			color: #000;

			.item {
				position: relative;
				padding: 0 40rpx;
				font-size: 28rpx;
				margin-bottom: 20rpx;

				.dot {
					position: absolute;
					top: 60rpx;
					right: 52rpx;
					width: 16rpx;
					height: 16rpx;
					border-radius: 50%;
					background-color: red;
					z-index: 999;
				}

				.img {
					position: absolute;
					top: 66rpx;
					right: 60rpx;
					width: 34rpx;
				}

				.title {
					padding-bottom: 10rpx;
					border-bottom: 2rpx solid #c7c7c9;
					color: #91743E;
				}

				.brief {
					.brief-item {
						width: 100%;
					}
				}

				.sendTime {
					width: 100%;
					text-align: center;
					margin-bottom: 10rpx;
					color: #c7c7c9;
				}

				.container {
					padding: 22rpx;
					background: #FFFFFF;
					box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(0, 0, 0, 0.05);
					border-radius: 10rpx 10rpx 10rpx 10rpx;

					.title {
						display: flex;
						font-weight: 700;
						overflow: hidden;
						font-size: 32rpx;
					}

					.brief {
						margin-top: 20rpx;
					}

					.times {
						margin-top: 26rpx;
						font-size: 28rpx;
						color: #606060;
					}
				}
			}

		}
	}

	.noData {
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translateX(-50%) translateY(-50%);
		width: 100%;

		.img {
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;

			image {
				width: 166rpx;
				height: 153rpx;
			}

			.tex {
				font-size: 32rpx;
				font-family: Source Han Sans CN-Regular, Source Han Sans CN;
				font-weight: 400;
				color: #525252;
			}
		}

	}

	.search-box {
		margin: 15rpx 0;
		// display: flex;
		// justify-content: space-around;
		// align-items: center;

		.search-item {
			margin: 0rpx auto;
			padding-top: 10rpx;
			display: flex;
			justify-content: space-around;
			align-items: center;
			// box-sizing: border-box;

			.oneitem {
				width: 680rpx;
				height: 80rpx;
				opacity: 1;
				border-radius: 40rpx;
				background: rgba(255, 255, 255, 1);
				box-shadow: 0rpx 6rpx 12rpx rgba(0, 0, 0, 0.18);
				display: flex;
				justify-content: flex-start;
				align-items: center;
				padding: 0rpx 20rpx;
				box-sizing: border-box;
				margin: 0rpx auto;
			}

			.twoitem {
				margin: 0rpx auto;
				width: 540rpx;
				height: 80rpx;
				opacity: 1;
				border-radius: 40rpx;
				background: rgba(255, 255, 255, 1);
				box-shadow: 0rpx 6rpx 12rpx rgba(0, 0, 0, 0.18);
				display: flex;
				justify-content: flex-start;
				align-items: center;
				padding: 0rpx 20rpx;
				box-sizing: border-box;
			}
		}

		.searchbutton {
			font-size: 28rpx;
			font-family: Source Han Sans CN-Regular, Source Han Sans CN;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 64rpx;
			width: 120rpx;
			height: 64rpx;
			opacity: 1;
			border-radius: 32rpx;
			background: linear-gradient(270deg, rgba(136, 172, 255, 1) 0%, rgba(33, 101, 255, 1) 100%);


		}
	}
</style>